﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BYFW.BOL
{
    /// <summary>
    /// 语言资源文件
    /// </summary>
    public class LanguageResource
    {
        static LanguageResource()
        {
            Init();
        }

        //BOL
        /// <summary>
        /// Name属性无效
        /// </summary>
        public const string EX_BOL_NameNotAllowedErr = "EX_BOL_NameNotAllowedErr";
        /// <summary>
        /// Comment属性无效
        /// </summary>
        public const string EX_BOL_CommentNotAllowedErr = "EX_BOL_CommentNotAllowedErr";
        /// <summary>
        /// TS属性无效
        /// </summary>
        public const string EX_BOL_TSNotAllowedErr = "EX_BOL_TSNotAllowedErr";
        /// <summary>
        /// GUID属性无效
        /// </summary>
        public const string EX_BOL_GuidNotAllowedErr = "EX_BOL_GuidNotAllowedErr";
        /// <summary>
        /// Code属性无效
        /// </summary>
        public const string EX_BOL_CodeNotAllowedErr = "EX_BOL_CodeNotAllowedErr";
        /// <summary>
        /// 数据库连接错误
        /// </summary>
        public const string EX_BOL_ConnectDBErr = "EX_BOL_ConnectDBErr";
        /// <summary>
        /// 字段不存在错误
        /// </summary>
        public const string EX_BOL_FieldNeedErr = "EX_BOL_FieldNeedErr";
        /// <summary>
        /// Code为空
        /// </summary>
        public const string EX_BOL_CodeIsEmptyErr = "EX_BOL_CodeIsEmptyErr";
        /// <summary>
        /// Code已存在
        /// </summary>
        public const string EX_BOL_CodeExistErr = "EX_BOL_CodeExistErr";
        /// <summary>
        /// 转换到Bool类型失败
        /// </summary>
        public const string EX_BOL_ConvertToBoolErr = "EX_BOL_ConvertToBoolErr";
        /// <summary>
        /// 转换到Int类型失败
        /// </summary>
        public const string EX_BOL_ConvertToIntErr = "EX_BOL_ConvertToIntErr";
        /// <summary>
        /// 转换到Double类型失败
        /// </summary>
        public const string EX_BOL_ConvertToDoubleErr = "EX_BOL_ConvertToDoubleErr";
        /// <summary>
        /// 转换到DateTime类型失败
        /// </summary>
        public const string EX_BOL_ConvertToDateTimeErr = "EX_BOL_ConvertToDateTimeErr";
        /// <summary>
        /// 转换到Byte[]类型失败
        /// </summary>
        public const string EX_BOL_ConvertToByteArrayErr = "EX_BOL_ConvertToByteArrayErr";
        /// <summary>
        /// 转换到TS失败
        /// </summary>
        public const string EX_BOL_ConvertToTSErr = "EX_BOL_ConvertToTSErr";
        /// <summary>
        /// 转换到GUID失败
        /// </summary>
        public const string EX_BOL_ConvertToGuidErr = "EX_BOL_ConvertToGuidErr";

        //DAL
        /// <summary>
        /// 事物不存在
        /// </summary>
        public const string EX_DAL_TransInvalidErr = "EX_DAL_TransInvalidErr";
        /// <summary>
        /// 数据库不允许TS类型字段
        /// </summary>
        public const string EX_DAL_TSNotAllowedErr = "EX_DAL_TSNotAllowedErr";
        /// <summary>
        /// 数据库不允许事物
        /// </summary>
        public const string EX_DAL_TransactionNotAllowedErr = "EX_DAL_TransactionNotAllowedErr";

        //Model-Register
        /// <summary>
        /// 注册信息过期
        /// </summary>
        public const string EX_Model_Register_ExpiredErr = "EX_Model_Register_ExpiredErr";
        /// <summary>
        /// 注册信息验证失败
        /// </summary>
        public const string EX_Model_Register_FailErr = "EX_Model_Register_FailErr";
        /// <summary>
        /// 验证服务器错误
        /// </summary>
        public const string EX_Model_Register_ServerFailErr = "EX_Model_Register_ServerFailErr";
        /// <summary>
        /// 已是最新版
        /// </summary>
        public const string EX_Model_Register_IsLatestErr = "EX_Model_Register_IsLatestErr";
        /// <summary>
        /// 注册信息被其他机器占用
        /// </summary>
        public const string EX_Model_Register_OccupyErr = "EX_Model_Register_OccupyErr";
        /// <summary>
        /// 网络错误
        /// </summary>
        public const string EX_Model_Register_NetErr = "EX_Model_Register_NetErr";


        private static ELanguage s_language = ELanguage.Chinese;
        private static Dictionary<string, LanguageItem> m_items = new Dictionary<string, LanguageItem>();

        /// <summary>
        /// 全局使用的语言种类
        /// </summary>
        public static ELanguage Language
        {
            get
            {
                if (s_language == ELanguage.Default)
                {
                    s_language = ELanguage.Chinese;
                }
                return s_language;
            }
            set
            {
                s_language = value;
            }
        }
        /// <summary>
        /// 语言包条目
        /// </summary>
        internal static Dictionary<string, LanguageItem> Items
        {
            get { return m_items; }
        }
        /// <summary>
        /// 初始化语言条目
        /// </summary>
        private static void Init()
        {
            m_items.Clear();
            //BOL
            m_items.Add(EX_BOL_NameNotAllowedErr, new LanguageItem("类{0}的Name属性无效！", ""));
            m_items.Add(EX_BOL_CommentNotAllowedErr, new LanguageItem("类{0}的Comment属性无效！", ""));
            m_items.Add(EX_BOL_TSNotAllowedErr, new LanguageItem("类{0}的TS属性无效！", ""));
            m_items.Add(EX_BOL_GuidNotAllowedErr, new LanguageItem("类{0}的Guid属性无效！", ""));
            m_items.Add(EX_BOL_CodeNotAllowedErr, new LanguageItem("类{0}的Code属性无效！", ""));
            m_items.Add(EX_BOL_ConnectDBErr, new LanguageItem("连接数据库发生错误：{0}", ""));
            m_items.Add(EX_BOL_FieldNeedErr, new LanguageItem("数据库表 {0} 中字段 {1} 不存在！", ""));
            m_items.Add(EX_BOL_CodeIsEmptyErr, new LanguageItem("类{0}的Code属性为空！", ""));
            m_items.Add(EX_BOL_CodeExistErr, new LanguageItem("类{0}的Code属性已存在！", ""));
            m_items.Add(EX_BOL_ConvertToBoolErr, new LanguageItem("数据库表 {0} 中字段 {1} 转换为Bool类型失败！", ""));
            m_items.Add(EX_BOL_ConvertToIntErr, new LanguageItem("数据库表 {0} 中字段 {1} 转换为Int类型失败！", ""));
            m_items.Add(EX_BOL_ConvertToDoubleErr, new LanguageItem("数据库表 {0} 中字段 {1} 转换为Double类型失败！", ""));
            m_items.Add(EX_BOL_ConvertToDateTimeErr, new LanguageItem("数据库表 {0} 中字段 {1} 转换为DateTime类型失败！", ""));
            m_items.Add(EX_BOL_ConvertToByteArrayErr, new LanguageItem("数据库表 {0} 中字段 {1} 转换为Byte[]类型失败！", ""));
            m_items.Add(EX_BOL_ConvertToTSErr, new LanguageItem("数据库表 {0} 中字段 {1} 转换为TS失败！", ""));
            m_items.Add(EX_BOL_ConvertToGuidErr, new LanguageItem("数据库表 {0} 中字段 {1} 转换为Guid失败！", ""));
            //DAL
            m_items.Add(EX_DAL_TransInvalidErr, new LanguageItem("事务集合中不包含ID为[{0}]的事务！", ""));
            m_items.Add(EX_DAL_TSNotAllowedErr, new LanguageItem("数据库操作类{0}不支持时间戳！", ""));
            m_items.Add(EX_DAL_TransactionNotAllowedErr, new LanguageItem("数据库操作类{0}不支持事务！", ""));
            //Model-Register
            m_items.Add(EX_Model_Register_ExpiredErr, new LanguageItem("注册信息已过期！", ""));
            m_items.Add(EX_Model_Register_FailErr, new LanguageItem("注册信息验证失败！", ""));
            m_items.Add(EX_Model_Register_ServerFailErr, new LanguageItem("服务器出现故障！", ""));
            m_items.Add(EX_Model_Register_IsLatestErr, new LanguageItem("已是最新版！", ""));
            m_items.Add(EX_Model_Register_OccupyErr, new LanguageItem("注册信息被占用！", ""));
            m_items.Add(EX_Model_Register_NetErr, new LanguageItem("网络连接出现故障！", ""));
        }

        /// <summary>
        /// 使用全局设置的语言
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetInfo(string id, params object[] args)
        {
            return GetInfo(id, Language, args);
        }
        /// <summary>
        /// 获取指定语言的信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public static string GetInfo(string id, ELanguage language, params object[] args)
        {
            if (Items.ContainsKey(id))
            {
                switch (language)
                {
                    case ELanguage.English:
                        return string.Format(Items[id].EN, args);
                    default:
                        return string.Format(Items[id].ZH, args);
                }
            }
            return id;
        }

        protected void Test()
        {
            //string a = GetInfo("EX_BOL_ConvertToGuidErr", "nihao");
            //Console.WriteLine(a);
            string ori = @"
            EX_BOL_NameNotAllowedErr', new LanguageItem('类{0}的Name属性无效！', ''));
            EX_BOL_CommentNotAllowedErr', new LanguageItem('类{0}的Comment属性无效！', ''));
            EX_BOL_TSNotAllowedErr', new LanguageItem('类{0}的TS属性无效！', ''));
            EX_BOL_GuidNotAllowedErr', new LanguageItem('类{0}的Guid属性无效！', ''));
            EX_BOL_CodeNotAllowedErr', new LanguageItem('类{0}的Code属性无效！', ''));
            EX_BOL_ConnectDBErr', new LanguageItem('连接数据库发生错误：{0}', ''));
            EX_BOL_FieldNeedErr', new LanguageItem('数据库表 {0} 中字段 {1} 不存在！', ''));
            EX_BOL_CodeIsEmptyErr', new LanguageItem('类{0}的Code属性为空！', ''));
            EX_BOL_CodeExistErr', new LanguageItem('类{0}的Code属性已存在！', ''));

            EX_BOL_ConvertToBoolErr', new LanguageItem('数据库表 {0} 中字段 {1} 转换为Bool类型失败！', ''));
            EX_BOL_ConvertToIntErr', new LanguageItem('数据库表 {0} 中字段 {1} 转换为Int类型失败！', ''));
            EX_BOL_ConvertToDoubleErr', new LanguageItem('数据库表 {0} 中字段 {1} 转换为Double类型失败！', ''));
            EX_BOL_ConvertToDateTimeErr', new LanguageItem('数据库表 {0} 中字段 {1} 转换为DateTime类型失败！', ''));
            EX_BOL_ConvertToByteArrayErr', new LanguageItem('数据库表 {0} 中字段 {1} 转换为Byte[]类型失败！', ''));
            EX_BOL_ConvertToTSErr', new LanguageItem('数据库表 {0} 中字段 {1} 转换为TS失败！', ''));
            EX_BOL_ConvertToGuidErr', new LanguageItem('数据库表 {0} 中字段 {1} 转换为Guid失败！', ''));

            EX_DAL_TransInvalidErr', new LanguageItem('事务集合中不包含ID为[{0}]的事务！', ''));
            EX_DAL_TSNotAllowedErr', new LanguageItem('数据库操作类{0}不支持时间戳！', ''));
            EX_DAL_TransactionNotAllowedErr', new LanguageItem('数据库操作类{0}不支持事务！', ''));

            EX_Model_Register_ExpiredErr', new LanguageItem('注册信息已过期！', ''));
            EX_Model_Register_FailErr', new LanguageItem('注册信息验证失败！', ''));
            EX_Model_Register_ServerFailErr', new LanguageItem('服务器出现故障！', ''));
            EX_Model_Register_IsLatestErr', new LanguageItem('已是最新版！', ''));
            EX_Model_Register_OccupyErr', new LanguageItem('注册信息被占用！', ''));
            EX_Model_Register_NetErr', new LanguageItem('网络连接出现故障！', ''));";
            string[] t = ori.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < t.Length; i++)
            {
                int idx = t[i].IndexOf("'");
                string fmt = string.Format("public const string {0} = \"{0}\";", t[i].Remove(idx).Trim());
                //Console.WriteLine(fmt);
            }
            for (int i = 0; i < t.Length; i++)
            {
                int idx = t[i].IndexOf("'");
                string fmt = string.Format("m_items.Add({0}{1}",t[i].Remove(idx).Trim(), t[i].Substring(idx+1).Replace('\'','"'));
                Console.WriteLine(fmt);
            }
        }
    }
}
